//---------------------------------------------
// Shadow
#ifdef GROUND_SHADOW

  #if defined(MultiShadow) || defined(PointGroundShadows)
  //layout(std430) readonly buffer ShadowBuffer
  //{
  //  ShadowData_t g_shadowBuffer[];
  //};
  #endif //MultiShadow


  in vec4 vShadowTexCoord;
  #ifdef MultiGroundShadow
      float intensity[8] =
      {
        cb_Ground.u_ShadowsIntensity.x,
        cb_Ground.u_ShadowsIntensity.y,
        cb_Ground.u_ShadowsIntensity.z,
        cb_Ground.u_ShadowsIntensity.w,
        cb_Ground.u_ShadowsIntensity2.x,
        cb_Ground.u_ShadowsIntensity2.y,
        cb_Ground.u_ShadowsIntensity2.z,
        cb_Ground.u_ShadowsIntensity2.w
      };

      float blur[8] =
      {
        cb_Ground.u_ShadowsBlurDelta.x,
        cb_Ground.u_ShadowsBlurDelta.y,
        cb_Ground.u_ShadowsBlurDelta.z,
        cb_Ground.u_ShadowsBlurDelta.w,
        cb_Ground.u_ShadowsBlurDelta2.x,
        cb_Ground.u_ShadowsBlurDelta2.y,
        cb_Ground.u_ShadowsBlurDelta2.z,
        cb_Ground.u_ShadowsBlurDelta2.w
      };
  #endif

  vec4 GetShadowMapCoord(int iLightID, int iShadowID)
  {
    vec4 coord;
	
    #if defined(MultiGroundShadow) || defined(MultiShadow)
     mat4 passageMatrix = _sbcbMultiShadow.PassageMatrix;
	 mat4 projMatrix = g_shadowBuffer[GROUND_FIRST_SHADOW + iShadowID].ObjectViewToLightProjMatrix * passageMatrix;
	#else
	 mat4 projMatrix = _sbcbShadow.ObjectViewToLightViewMatrix;
	#endif

	#ifdef UsePassThroughTransition
	 coord = projMatrix * _vPrivGetViewInverseTransition() * vShadowTexCoord;
	#else
	 coord = projMatrix * vShadowTexCoord;
	#endif

	#ifdef MultiGroundShadow
	 coord.xy = vFma(coord.xy / coord.w, vec2(blur[iLightID] * 0.5), vec2(0.5));  
	#else
	 coord.xy = vFma(coord.xy/coord.w, vec2(cb_Ground.u_ShadowsBlurDelta.x*0.5), vec2(0.5));
	#endif

	return coord;
  }

  float GetGroundShadowRatio()
  {
    float shadowRatio = 1.0;

    #ifdef MultiGroundShadow
      // iOS WARNING: this code path should not run on iOS. If it does, you have to handle the ClampToBorder case!!

      shadowRatio = 0.0;
	  int lightID = 0;
	  int shadowID = 0;

	  for(int i = 0 ; i < NonPointGroundShadows; ++i)
	  {
		vec4 coord = GetShadowMapCoord(lightID, i);
			
		if(PointGroundShadows >= 1 && i==0)
		 shadowRatio += (1.0 - vTexture(shadowMapTex2, vec3(coord.xy, i)).x * (intensity[lightID] + (PointGroundShadows * 2.0)));
		else
   		 shadowRatio += (1.0 - vTexture(shadowMapTex2, vec3(coord.xy, i)).x * intensity[lightID]);
			
        lightID++;  
	  }

	  shadowID = NonPointGroundShadows;
	  int layer=0;

	  #ifdef NB_CASCADES
	  layer += NB_CASCADES-1;
	  #endif

	  for(int i = 0 ; i < PointGroundShadows; ++i)
	  {
		for(int j = 0 ; j < 6; j++) //for 6 faces of point light
		{
		  vec4 coord = GetShadowMapCoord(lightID, shadowID + layer);
          shadowRatio += (1.0 - vTexture(shadowMapTex2, vec3(coord.xy, shadowID)).x * (intensity[lightID] + (PointGroundShadows)));
		  shadowID++;
		}
		lightID++;
	  }

      shadowRatio /= float(MultiGroundShadow);
            
    #else //Mono shadow
	  vec4 coord = GetShadowMapCoord(0,0);
      shadowRatio = (1.0 - vTexture(shadowMapTex2, coord.xy).x * cb_Ground.u_ShadowsIntensity.x);
    #endif //MultiShadow
    
   return shadowRatio;
  }
  
#endif
